package com.stms.service.impl;

import com.stms.dao.SendClientRepository;
import com.stms.dao.ContractRepository;
import com.stms.pojo.Contract;
import com.stms.pojo.SendClient;
import com.stms.service.ContractService;
import com.stms.utils.LogisticsPriceUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.sql.Date;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Service
public class ContractServiceImpl implements ContractService {
    @Autowired
    public ContractRepository contractRepository;
    @Autowired
    public SendClientRepository sendClientRepository;

    @Override
    public boolean addContract(Contract contract) {
        try {
            Double totalCost = LogisticsPriceUtil.cost(contract.getStandardWeight(), contract.getWeightUnitCost(),
                    contract.getDistanceUnitCost(), contract.getContinuedWeight(), contract.getWeight(),
                    contract.getValue(), contract.getDistance());
            contract.setCost(totalCost);

            if (contractRepository.save(contract)!=null){
                return true;
            }else{
                return false;
            }
        }catch(Exception e){
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean editContract(Contract contract) {
        if(contractRepository.save(contract)!=null){
            return true;
        }else{
            return false;
        }
    }

    @Override
    public void deleteBatch(String contractIds) {
        String[] contractStr = contractIds.split(",");
        List<Integer> list = new ArrayList<Integer>();

        for(String str:contractStr){
            list.add(Integer.valueOf(str));
        }
        contractRepository.deleteBatch(list);
    }

    public Page<Contract> fuzzyQuery(Integer pageNum, Integer pageSize,String direction, Contract contract) {
        if(pageNum==null||pageNum==0){
            pageNum = 1;
        }
        pageNum = pageNum-1;
        if(pageSize==null){
            pageSize = 10;
        }
        if(direction==null){
            direction = new String("DESC");
        }
        Date beginDate = contract.getStartDate();
        Date endDate = contract.getEndDate();
        //排序、分页初始化
        Sort.Direction direc = direction.equals("DESC")?Sort.Direction.DESC:Sort.Direction.ASC;
        Pageable pageable = PageRequest.of(pageNum,pageSize, direc,"id");

        Page<Contract> page = contractRepository.findAll(new Specification<Contract>() {
            @Override
            public Predicate toPredicate(Root<Contract> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                //创建条件集合
                List<Predicate> list = new ArrayList<>();
                if (contract == null) {
                    return null;
                }
                if (contract.getId() != null && !contract.getId().equals("")) {
                    list.add(cb.equal(root.get("id"), contract.getId()));
                }
//                if (contract.getSendCustomer() != null && !contract.getSendCustomer().equals("")) {
//                    list.add(cb.like(root.get("sendCustomer").as(String.class), "%" + contract.getSendCustomer().trim() + "%"));
//                }
                if (beginDate != null && !beginDate.equals("")) {
                    list.add(cb.greaterThanOrEqualTo(root.get("sendDate"), beginDate));
                }
                if (endDate != null && !endDate.equals("")) {
                    list.add(cb.lessThanOrEqualTo(root.get("sendDate"), endDate));
                }
                Predicate[] predicate = new Predicate[list.size()];
                return cb.and(list.toArray(predicate));
            }
        }, pageable);
        return page;
    }

    @Override
    public Page<Contract> findByPage(Integer pageNum, Integer pageSize) {
        if(pageNum==null||pageNum==0){
            pageNum = 1;
        }
        pageNum = pageNum-1;
        if(pageSize==null){
            pageSize = 10;
        }

        PageRequest of = PageRequest.of(pageNum,pageSize);
        Page<Contract> page = contractRepository.findAll(of);
        return page;
    }
}
